package com.heartech.prediciton_wx.utils.message;

import com.heartech.prediciton_wx.entity.message.*;
import lombok.extern.slf4j.Slf4j;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
/**
 * 参数：继承自baseMessage基类的子类对象
 * 返回值：String类型，生成的xml数据需要string类型的
 */

@Slf4j
public class XmlHandle {
//    文本信息回复
    public static <T extends BaseMessage> String XmlHandleFun(T object) {

        //Document对象，后续生成xml结构，并调用其方法进行string类型数据返回
        Document dou = null;
        boolean isif = true;
        try {
            //开始创建dom结构
            dou = DocumentHelper.createDocument();
            //dou.addElement  创建唯一的全局父节点，根据官方文档的格式，返回的 xml 格式基本只有最多3级
            Element root = dou.addElement("xml");
            /* root.addElement 在root节点下，创建一个节点，相当于二级节点 <ToUserName></ToUserName>
             * addText 为添加二级节点的内容，<ToUserName>内容</ToUserName>
             * 补充知识：如果要给此行添加xml属性，使用如下代码-> root.addAttribute("id", "属性");补充知识：如果要给此行添加xml属性，使用如下代码-> root.addAttribute("id", "属性"); 值为<xml id="属性"></xml>
             * 获取对象的值，进行字符串拼接
             * */
            Element emp = root.addElement("ToUserName").addCDATA(object.getToUserName());
            Element emp1 = root.addElement("FromUserName").addCDATA(object.getFromUserName());
            Element emp2 = root.addElement("CreateTime").addText(String.valueOf(object.getCreateTime()));
            Element emp3 = root.addElement("MsgType").addCDATA(object.getMsgType());
            /* 判断传入的对象是否是它的实例,是的话进行转型，并添加属于自己类的特有的属性 */
            if (object instanceof TextMessage && isif) {
                TextMessage textMessage = (TextMessage) object;
                Element emp4 = root.addElement("Content").addCDATA(textMessage.getContent());
                isif = false;
            }
            if (object instanceof ImageMessage && isif) {
                ImageMessage imageMessage = (ImageMessage) object;
                Element emp4 = root.addElement("Image");
                emp4.addElement("MediaId").addCDATA(imageMessage.getMediaId());
                isif = false;
            }
            if (object instanceof VoiceMessage && isif) {
                VoiceMessage voiceMessage = (VoiceMessage) object;
                Element emp4 = root.addElement("Voice");
                emp4.addElement("MediaId").addCDATA(voiceMessage.getMediaId());
                isif = false;
            }
            if (object instanceof VideoMessage && isif) {
                VideoMessage videoMessage = (VideoMessage) object;
                Element emp4 = root.addElement("Video");
                emp4.addElement("MediaId").addCDATA(videoMessage.getMediaId());
                isif = false;
            }
        } catch (Exception e) {
            log.error("出现错误！XmlHandleFun");
            e.printStackTrace();
        }

        int count = "encoding=\"UTF-8\"?".length();
        String result = dou.asXML();
        result = result.substring(result.indexOf("encoding=\"UTF-8\"?") + count + 1);
        return result.trim();
//        return  dou.asXML();
    }

    /* 扫码回复文本信息 */
    public static <T extends BaseMessage> String XmlHandleFunEvent(T object) {
        Document dou = null;
        boolean isif = true;
        try {
            dou = DocumentHelper.createDocument();
            Element root = dou.addElement("xml");
            Element emp = root.addElement("ToUserName").addCDATA(object.getToUserName());
            Element emp1 = root.addElement("FromUserName").addCDATA(object.getFromUserName());
            Element emp2 = root.addElement("CreateTime").addText(String.valueOf(object.getCreateTime()));
            Element emp3 = root.addElement("MsgType").addCDATA("text");

            if (object instanceof TextMessage && isif) {
                TextMessage textMessage = (TextMessage) object;
                Element emp4 = root.addElement("Content").addCDATA(textMessage.getContent());
                isif = false;
            }

        } catch (Exception e) {
            log.error("出现错误！XmlHandleFun");
            e.printStackTrace();
        }
        int count = "encoding=\"UTF-8\"?".length();
        String result = dou.asXML();
        result = result.substring(result.indexOf("encoding=\"UTF-8\"?") + count + 1);
        return result.trim();

    }

    /* 扫码回复图文消息 */
    public static <T extends BaseMessage> String XmlHandleFunEventPic(T object) {
        Document dou = null;
        boolean isif = true;
        try {
            // 图文消息标题
            String Title = "HearTech";
            // 图文消息描述
            String Description = "请填写您的信息，我们将竭诚为您服务！";
            // 图片链接
            String PicUrl = "https://heartech-test.tech/test/static/index.png";

            dou = DocumentHelper.createDocument();
            Element root = dou.addElement("xml");
            Element emp = root.addElement("ToUserName").addCDATA(object.getToUserName());
            Element emp1 = root.addElement("FromUserName").addCDATA(object.getFromUserName());
            Element emp2 = root.addElement("CreateTime").addText(String.valueOf(object.getCreateTime()));
            Element emp3 = root.addElement("MsgType").addCDATA("news");
            Element emp4 = root.addElement("ArticleCount").addText("1");
            Element articles = root.addElement("Articles");
            Element item = articles.addElement("item");
            item.addElement("Title").addCDATA(Title);
            item.addElement("Description").addCDATA(Description);
            item.addElement("PicUrl").addCDATA(PicUrl);
            if (object instanceof TextMessage && isif) {
                TextMessage textMessage = (TextMessage) object;
                // 点击图文消息跳转链接
                item.addElement("Url").addCDATA(textMessage.getContent());
                isif = false;
            }
        } catch (Exception e) {
            log.error("出现错误！XmlHandleFun", e);
        }
        int count = "encoding=\"UTF-8\"?".length();
        String result = dou.asXML();
        result = result.substring(result.indexOf("encoding=\"UTF-8\"?") + count + 1);
        return result.trim();
    }

}
